using System.Linq;
using System.Net.Mail;
using StructureMap;

namespace CxRomos.Core.Services.Implementation {
    [Pluggable("Default")]
    public class Email : IEmail {
        private const string ToEmailAddress = "danijel.kecman@gmail.com";
        private const string FromEmailAddress = "danijel.kecman@gmail.com";
        private readonly IConfiguration _configuration;

        public Email() {
            _configuration = ObjectFactory.GetInstance<IConfiguration>();
        }

        public void SendEmail(string from, string subject, string message) {
            var mm = new MailMessage(from, ToEmailAddress) {Subject = subject, Body = message};

            Send(mm);
        }

        public void SendEmail(string to, string cc, string bcc, string subject, string message) {
            var mm = new MailMessage(FromEmailAddress, to);
            mm.CC.Add(cc);
            mm.Bcc.Add(bcc);
            mm.Subject = subject;
            mm.Body = message;
            mm.IsBodyHtml = true;

            Send(mm);
        }

        public void SendEmail(string[] to, string[] cc, string[] bcc, string subject, string message) {
            var mm = new MailMessage();

            foreach (var t in to) {
                mm.To.Add(t);
            }
            foreach (var c in cc) {
                mm.CC.Add(c);
            }
            foreach (var b in bcc) {
                mm.Bcc.Add(b);
            }

            mm.From = new MailAddress(FromEmailAddress);
            mm.Subject = subject;
            mm.Body = message;
            mm.IsBodyHtml = true;

            Send(mm);
        }

        public void SendIndividualEmailPerRecipient(string[] to, string subject, string message) {
            foreach (var mm in to.Select(t => new MailMessage(FromEmailAddress, t))) {
                mm.Subject = subject;
                mm.Body = message;
                mm.IsBodyHtml = true;

                Send(mm);
            }
        }

        public void SendPasswordReminderEmail(string to, string encryptedPassword, string username) {
            var message = "Here is the password you requested: " + Cryptography.Decrypt(encryptedPassword, username);
            SendEmail(to, "", "", "Password Reminder", message);
        }

        public void SendEmailAddressVerificationEmail(string username, string to) {
            var msg = "Please click on the link below or paste it into a browser to verify your email account.<BR><BR>" +
                            "<a href=\"" + _configuration.RootUrl + "Account/VerifyEmail.aspx?a=" +
                            Cryptography.Encrypt(username, "verify") + "\">" +
                            _configuration.RootUrl + "Account/VerifyEmail.aspx?a=" +
                            Cryptography.Encrypt(username, "verify") + "</a>";

            SendEmail(to, "", "", "Account created! Email verification required.", msg);
        }

        private static void Send(MailMessage message) {
            var smtpClient = new SmtpClient();
            // TODO: Live
            //smtpClient.Send(message);
        }
    }
}